package com.kittlen.managementSystem.interceptor;

import com.kittlen.managementSystem.utils.ObjectUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author kittlen
 * 用于拦截当其他用户访问管理员页面时用于权限判断
 * @version 1.0
 * @date 2020/8/2 16:23
 */
public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        Object user=httpServletRequest.getSession().getAttribute("user");
        Object type=httpServletRequest.getSession().getAttribute("type");
        if(ObjectUtil.isEmpty(user)||ObjectUtil.isEmpty(type)){
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/adminLogin.html");
            return true;
        }
        if(((int)type)!=9){
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/adminLogin.html");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
